const { getCount, query } = require('../../utils/dbTools');
const { postsVisitCountUpdate, getPostsList, getPostsInfo, addPosts, updatePosts } = require('../../model/posts')
const tableName = 'posts';
/**
 * 获取数据总条数
 * 
*/

const postsTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 查询条件
    var map = {}
    const result = await getCount(tableName, map);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 *  文章搜索
 * 
*/
const postsSearch = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { keyword } = req.query;
    if (!keyword) {
        output.code = 500;
        output.msg = "参数错误,缺少参数keyword";
        return res.send(output);
    }
    const sql = `select * from ${tableName} where title like '%${keyword}%'`
    const result = await query(sql, {});
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 *  文章列表
 * 
*/
const postsList = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { page, size } = req.query;
    // 如果传递了分页参数, 则实现分页查询
    const map = page && size ? { page, size } : {}
    // 获取登录账号id
    if (req.loginInfo?.id) {
        map.uid = req.loginInfo?.id
    }
    const result = await getPostsList(map);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取文章详情
 * 
*/
const postsInfo = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    const uid = req.loginInfo.id
    const map = { id, uid }
    const info = await getPostsInfo(map);
    if (info) {
        output.data = info;
        // 更新文章的点击次数
        await postsVisitCountUpdate(id)
    } else {
        output.code = 404;
        output.msg = "文章id错误: 未查询到相关数据";
    }
    res.send(output);
}

/**
 * 发布文章
 * 
*/
const postsAdd = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const payload = { ...req.body };
    if (req.file) {
        payload.cover = getUploadedUrl(req.file);
    }
    const result = await addPosts(payload);
    output.data = result
    res.send(output);
}


/**
 * 更新文章
 * 
*/
const postsUpdate = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const payload = { ...req.body };
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    if (req.file) {
        payload.cover = getUploadedUrl(req.file);
    }
    const result = await updatePosts(payload);
    output.data = result
    res.send(output);
}

module.exports = {
    postsAdd,
    postsUpdate,
    postsList,
    postsTotal,
    postsInfo,
    postsSearch
}